<?php 
/*************************************************************************************
 * showtrippoints.php
 * -----------
 * phpGMapTrips 
 * 
 * This php application allows you to easily record your travel destinations 
 * and display them with Google maps.
 * 
 * Author: Stephane Coste (lescoste@gmail.com)
 * Copyright: (c) 2006 Stephane Coste (http://lescoste.net/)
 * Release Version: 0.1
 * CVS Revision Version: $Revision: 1.22 $
 * Date Started: 2006/02/19
 * Last Modified: $Date: 2007/03/14 14:32:43 $
 *
 *************************************************************************************
 *
 *     This file is part of phpGMapTrips.
 *
 *   phpGMapTrips is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   phpGMapTrips is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with phpGMapTrips; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ************************************************************************************/
?>
<?php 
	header("Content-type: text/xml");
	/*
	return trip points in xml
	*/
	$showtrip = urldecode($_GET["showtrip"]);
	$search = urldecode($_GET["search"]);
        if (preg_match( "/(\d+\.\d+\.\d+\.\d+)/", $search)) {
            // detect ip address in search field
            $visitorIp = $search;
            $search = "";
	    $showVisitor = "true";
        } else {
	    $showVisitor = urldecode($_GET["visitor"]);
        }
	$tripName = "All";
	if (!empty($showtrip)) {
		$tripName = $showtrip;
	}

	include_once("dbaccess.php");

	$isGroupOfTrips = false;
	if (!empty($search)) {
		$pointarray = searchPoints($search);
	} else if($showtrip == "Latest") {
		$datearray = getLatestPoints();
	} else {
		$pointarray = getTripPoints($showtrip);
                // group of trips
		if (count($pointarray) == 0) {
			$isGroupOfTrips = true;
			$pointarray = getGroupPoints($showtrip);
		}
                // load trip places order
		$opttripseqarray = getTripPointsOrder($showtrip);
		if (!empty($opttripseqarray)) {
			// check that no place has been removed from the trip
			$changedTripOrder = false;
			foreach($opttripseqarray[$showtrip] as $seq => $pointname) {
				if (!array_key_exists($pointname["name"], $pointarray)) {
					unset($opttripseqarray[$showtrip][$seq]);
					$changedTripOrder = true;
				}
			}
			if ($changedTripOrder) {
				$tmpopttripseqarray = array();
				$i = 0;
				foreach($opttripseqarray[$showtrip] as $seq => $pointname) {
					$tmpopttripseqarray[$i] = $pointname;
					$i++;
				}
				$opttripseqarray[$showtrip] = $tmpopttripseqarray;
			}
		}
		// load trip's groups
		$opttripgroups = getTripGroups($showtrip); 
	}

	echo '<trips>';
	echo '<trip name="'.$tripName.'" >';
	echo '<markers>';
	$index=0;

        $markerArray = array();

        if (!empty($search)) {
             foreach ($pointarray as $bookname => $book) {
		     if (isFound($book["name"], $search)) {
                        addMarker($book);
		     }
	     }
        } else {
	   if (!empty($showtrip) && $showtrip!= "Latest" && $showtrip!= "All" && $showtrip!="Visitor") {
		// all trip points
		printTrip($pointarray);
	   } else if ($showtrip== "All") {
		// all points
		foreach ($pointarray as $bookname => $book) {
                        addMarker($book);
		}
	   } else if ($showtrip== "Latest") {
		// all points
		foreach ($datearray as $bookname => $book) {
                        addMarker($book);
		}
	   } else {
		// visitor points
		include_once("showvisitor.php");
	   }
        }

             foreach ($markerArray as $book) {
			printMarker($book);
	     }


	echo '</markers>';
	if (!empty($opttripseqarray[$showtrip])) {
		echo '<order>';
		// all points
		foreach ($opttripseqarray[$showtrip] as $seq => $point) {
//			printOrder($point["name"], $point["seq"]);
			printOrder($point["name"], $seq);
		}
		//echo '<point name="" seq="">';
		echo '</order>';
	}
	if (!empty($opttripgroups)) {
		echo '<groups name="'.$opttripgroups.'"/>';
	}
	if ($isGroupOfTrips) {
		$tmpgroups = getGroups();
		echo '<grouptrips>';
		if (!empty($tmpgroups)) {
			foreach($tmpgroups[$showtrip] as $trip) {
				echo '<grouptrip name="'.$trip.'"/>';
			}
		}
		echo '</grouptrips>';
	}
	echo '</trip>';
	echo '</trips>';

function addMarker($tag) {
   global $markerArray;
   $key = "".$tag["lat"].$tag["long"];
   if (!array_key_exists($key, $markerArray)){ 
       $tmptagarray = array();
   } else {
       $tmptagarray = $markerArray[$key];
   }
   $tmptagarray[] = $tag;
   $markerArray[$key] = $tmptagarray;
}


	function printTrip($tag) {
		global $showVisitor, $visitorIp;
		foreach($tag as $book) {
                        addMarker($book);
		}
		if ($showVisitor == "true") {
			// visitor points
			include_once("showvisitor.php");
		}
	}

function printMarker($marker) {
	global $lang_zoom, $moreinfoNames, $index;


	echo '<!-- '.$index.' -->';
	$index++;
	echo '<marker name="'.$marker[0]["name"].'" title="'.$marker[0]["title"].'"  lat="'.$marker[0]["lat"].'" lng="'.$marker[0]["long"].'">';

	foreach($marker as $place) {

                echo '<place name="'.$place["name"].'" url="'.$place["url"].'" lat="'.$place["lat"].'" lng="'.$place["long"].'" ';
		echo ' trips="'.$place["trips"].'" ';


                foreach($moreinfoNames as $name) {
                   if (!empty($destiArray[$name])) {
	               echo ' '.$name.'="'.$place[$name].'" ';
                   }
                }
		foreach ($place["photos"] as $photo) {
			$photourl = $photo['url'];
			echo ' photoimg="'.$photourl.'" ';
		}   
		/*
		echo ' title="&lt;a href=\''.$place["url"].'\' style=\'text-decoration:none;\'  &gt;'.$place["name"].'';
		foreach ($place["photos"] as $photo) {
			$photourl = $photo['url'];
			if (!empty($photourl)) {
				echo ' &lt;br/&gt; ';
				echo ' &lt;img src=\''.$photourl.'\' style=\'border:1px solid black;height:100px;\' /&gt; ';
			}
		}   
		echo '&lt;/a&gt;" />';
		*/
		print " title=\"".$place["title"]."\" />";
	}
	echo '</marker>';
}

function printPoint($name, $lat,$long,$url,$photos, $trips, $book) {
  addMarker($book); 
}
	
	function printOrder($name, $seq) {
		echo '<point name="'.$name.'" seq="'.$seq.'" /> ';
	}
?>
